/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MOs.strategy.finiteDifference.BLO;

import MOs.strategy.finiteDifference.bean.*;
import MOs.strategy.finiteDifference.DAO.*;
import controller.java.Petri;
import data.java.Defs;
import java.lang.Math;

/**
 *
 * @author Erick Rubio Mendoza
 */
public class finiteDifferenceStrategyBLO {
    public void determineTypeMO(finiteDifferenceMO oFDMO, finiteDifferenceEnviroment oFDEnviroment)            
    {
        finiteDifferenceRulesBLO oFDRulesBLO = new finiteDifferenceRulesBLO();
        if(oFDRulesBLO.isKing(oFDMO, oFDEnviroment)){oFDMO.setType("King");}
        else if(oFDRulesBLO.isVIP(oFDMO, oFDEnviroment)){oFDMO.setType("VIP");}        
        else if(oFDRulesBLO.isSpecialForces(oFDMO, oFDEnviroment)){oFDMO.setType("SpecialForces");}
        else if(oFDRulesBLO.isSecurity(oFDMO, oFDEnviroment)){oFDMO.setType("Security");}
        else if(oFDRulesBLO.isCitizen(oFDMO, oFDEnviroment)){oFDMO.setType("Citizen");}
        else if(oFDRulesBLO.isHostage(oFDMO, oFDEnviroment)){oFDMO.setType("Hostage");}
        else {oFDMO.setType("Citizen");}
    }
    
    public boolean determineMitosis(finiteDifferenceMO oFDMO)
    {       
        //This is the mitosis condition by Finite Difference Method
        float Mo,NutMo;
        
        Mo = oFDMO.getEne()/2;
        NutMo=oFDMO.getNutrient()*(1-Defs.EAT_PERCENT) - 
                Defs.LESS_LIVE/Defs.EAT_PERCENT - 
                Defs.LESS_LIVE*
                (
                    (float)(
                        Math.log((double)(Defs.LESS_LIVE)) - 
                        Math.log((double)(Defs.EAT_PERCENT*oFDMO.getNutrient()*(1-Defs.EAT_PERCENT)))
                    )/
                    (float) Math.log((double)(1 - Defs.EAT_PERCENT))
                );
        
        if (Mo<NutMo)
        {
            return true;
        }   
        else 
        {
            return false;
        }
    }

    
    public void determineVectorAttack(finiteDifferenceEnviroment oFDEnviroment, finiteDifferenceMO oFDMO, finiteDifferenceDiferentialMove oFDDiferentialMove)
    {
        int dx,dy;
        finiteDifferenceMO oFDMOOpponent = new finiteDifferenceMO();
        oFDMOOpponent = oFDMO.getWeakEnemy();
        
        if(!finiteDifferenceRulesBLO.attackCondition(oFDMO,oFDMOOpponent))                
        {
            dx = 0;
            dy = 0;
        }
        else
        {    
            dx = oFDMOOpponent.getX()-oFDMO.getX();
            dy = oFDMOOpponent.getY()-oFDMO.getY();
        }
 
        oFDDiferentialMove.setDx(dx);
        oFDDiferentialMove.setDy(dy);
        
    }
    
            
    public void determineVectorMove(finiteDifferenceEnviroment oFDEnviroment, finiteDifferenceMO oFDMO, finiteDifferenceDiferentialMove oFDDiferentialMove)
    {
        int dx,dy;
        //if(finiteDifferenceRulesBLO.permanencyCondition(oFDMO) && oFDMO.getNutrient()>=oFDEnviroment.getMeanNutrients())
        //if(finiteDifferenceRulesBLO.permanencyCondition(oFDMO) && oFDMO.getNutrient()>=oFDEnviroment.getMaxBeneficePoint().getNutrientEpicentre())    
        //if(finiteDifferenceRulesBLO.permanencyCondition(oFDMO))
        if(finiteDifferenceRulesBLO.permanencyCondition(oFDMO) && oFDMO.getNutrient() >= 5.00f/6.00f*oFDEnviroment.getMaxNutrients())                
        {
            oFDDiferentialMove.setDx(0);
            oFDDiferentialMove.setDy(0);
        }
        else
        {    
            if (oFDEnviroment.getMaxBeneficePoint().getX()==oFDMO.getX())
            {
                dx=0;
            }
            else
            {
                dx = (oFDEnviroment.getMaxBeneficePoint().getX() - oFDMO.getX())/Math.abs(oFDEnviroment.getMaxBeneficePoint().getX() - oFDMO.getX());
            }

            if (oFDEnviroment.getMaxBeneficePoint().getY()==oFDMO.getY())
            {
                dy=0;
            }
            else
            {
                dy = (oFDEnviroment.getMaxBeneficePoint().getY() - oFDMO.getY())/Math.abs(oFDEnviroment.getMaxBeneficePoint().getY() - oFDMO.getY());
            }    

            finiteDifferenceMO oFDMOOpponent = new finiteDifferenceMO();
            oFDMOOpponent.setX(oFDMO.getX() + dx);
            oFDMOOpponent.setY(oFDMO.getY() + dy);

            finiteDifferenceMODAO oFDMODAO = new finiteDifferenceMODAO();
            oFDMODAO.MOAnalysis(oFDMOOpponent);

            if (finiteDifferenceRulesBLO.attackCondition(oFDMO,oFDMOOpponent))
            {
                oFDDiferentialMove.setDx(dx);
                oFDDiferentialMove.setDy(dy);            
            }
            else
            {
                oFDDiferentialMove.setDx(0);
                oFDDiferentialMove.setDy(0);
            }
        }
 
        //oFDDiferentialMove.setDx(0);
        //oFDDiferentialMove.setDy(0);
        
    }
     
}
